package com.lkm.market2_0.m_imple.steelyardserver.server;

import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;

import java.util.Arrays;



public abstract class AbsSendWriteContentTask extends AbsSendWriteTask {
    

    public AbsSendWriteContentTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, String id, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask) {
        super(taskExecutor, id, mTaskManager, bluetoothSocket, mReceiveTask);
    }










    
    protected abstract byte configFillNul();

    @Override
    protected byte[] onGetSendDataItem(int index) {


        byte [] data= new byte[32];
        Arrays.fill(data,configFillNul());

        if(!fillData(index,data)){
            return null;
        }


        
        int serialNo = serialNo();
        serialNo += index;
        
        byte[] serialNos = NumberHelp.intToBytesAsc(serialNo);




        
        
        byte contentCMD = serialNos[1];
        byte checkNum = contentCMD;
        for(int i=0;i<data.length;i++){
            checkNum+=data[i];
        }


        data = CollectionHelp.concatByte(new byte[]{(byte)0x55, (byte) 0xAA,serialNos[0], contentCMD},data);
        data = CollectionHelp.concatByte(data,new byte[]{(byte)00, (byte) 0x0D});
        data[data.length - 2] = checkNum;

        return data;
    }

    

    protected abstract boolean fillData(int index,byte[] data) ;
    protected abstract int serialNo() ;
}
